﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace GesturePlugin
{    
    /// <summary>
    /// Logique d'interaction pour GestureCanva.xaml
    /// </summary>
    public partial class GestureCanva : Window
    {
        private bool _draw = false;
        private int _gesturePrecision;
        private Point _lastPoint;
        private Point _currentPoint;
        private GesturesProcessing _gesture;
        // Liste contenant tous les points de la trajectoire effectué avec le pointeur
        private List<System.Windows.Point> _listePoints = new List<System.Windows.Point>();
        private PointCollection _myPointCollection = new PointCollection();

        public int GesturePrecision
        {
            get { return _gesturePrecision; }
            set { _gesturePrecision = value; }
        }


        public GestureCanva()
        {
            InitializeComponent();
            cadre.Height = Screen.PrimaryScreen.Bounds.Height;
            cadre.Width = Screen.PrimaryScreen.Bounds.Width;
            _gesture = new GesturesProcessing();
        }

        // Action lorsque le pointeur est enfoncé (mode sensoriel)
        public void CanDraw()
        {
            _draw = true;
            _listePoints.Clear();
            System.Drawing.Point lastPoint = System.Windows.Forms.Cursor.Position;
            _lastPoint = new Point(lastPoint.X, lastPoint.Y);
            _myPointCollection.Clear();
            _myPointCollection.Add(_lastPoint);
        }

        // Action lorsque le pointeur est en mouvement (mode sensoriel)
        public void DrawMouseMoves()
        {
            if (_draw)
            {
                _currentPoint = new Point(Mouse.GetPosition(this).X, Mouse.GetPosition(this).Y);
                //Add mouse positions in a list.
                _listePoints.Add(_currentPoint);

                //Define a geometric shape using the list of mouse positions.
                StreamGeometry geometricShape = new StreamGeometry();

                using (StreamGeometryContext ctx = geometricShape.Open())
                {
                    ctx.BeginFigure(_listePoints[0], true, false);
                    for (int i = 1; i < _listePoints.Count; i++)
                    {
                        ctx.LineTo(_listePoints[i], true, false);
                    }
                }

                //Draw the geometric shape
                Path myPath = new Path();

                myPath.Stroke = Brushes.Blue;
                myPath.StrokeThickness = 3;

                myPath.Data = geometricShape;
                cadre.Child = myPath;

                //
                Polyline myPolyline = new Polyline();
                myPolyline.Stroke = Brushes.Red;
                myPolyline.StrokeThickness = 2;

                double segmentDistance = GetDistance(_lastPoint, _currentPoint);
                if (segmentDistance >= _gesturePrecision)
                {
                    _myPointCollection.Add(_currentPoint);
                    myPolyline.Points = _myPointCollection;
                    border.Child = myPolyline;
                    _lastPoint = _currentPoint;
                }
            }
        }

        // Action lorsque le pointeur est relevé (mode sensoriel)
        public void CantDraw()
        {
           // label.Content = "Up";
           _draw = false;
        }

        /// <summary>
        /// Calculates distance between 2 points
        /// </summary>
        /// <param name="p1">First point</param>
        /// <param name="p2">Second point</param>
        /// <returns>Distance between two points</returns>
        private double GetDistance(Point p1, Point p2)
        {
            double dx = p1.X - p2.X;
            double dy = p1.Y - p2.Y;

            return Math.Sqrt(dx * dx + dy * dy);
        }
    }
}
